#include "widget.h"
#include "ui_widget.h"
#include "logon.h"
#include "modifypd.h"
#include "mysql_data.h"
#include "chat.h"
#include "brolist.h"

#include <QHostAddress>
#include <QMessageBox>
#include <QDebug>
#include <QPainter>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    setFixedSize(338,321);
    setWindowTitle("用户登录");
//    QPalette pal = this->palette();
//    pal.setBrush(QPalette::Background,QBrush(QPixmap("://p1.jpeg")));
//    setPalette(pal);
    ui->passwd->setEchoMode(QLineEdit::Password);
    //注册事件
    ui->login->installEventFilter(this);
    ui->updatepd->installEventFilter(this);
    //连接服务器
    m_t = new QTcpSocket(this);
    m_t->connectToHost(QHostAddress("127.0.0.1"), 8888);
    connect(m_t, &QTcpSocket::readyRead, this, [=](){
       int type;
       BroList *broList;
       m_t->read((char*)&type, 4);
       if (type == 1) {
           QByteArray data = m_t->readAll();
           QString back = data;
           if (QString::compare(back, "ok") == 0) {
               broList = new BroList(m_t, ui->num->text());
               broList->setAttribute(Qt::WA_DeleteOnClose, true);
               broList->show();
               setVisible(false);
               connect(this, &Widget::sendPcnt, broList, &BroList::recvPcnt);
               connect(this, &Widget::sendId, broList, &BroList::recvId);
               connect(this, &Widget::sendListOver, broList, &BroList::recvListOver);
               connect(this, &Widget::sendMsg, broList, &BroList::recvMsg);
               connect(this, &Widget::sendFileText, broList, &BroList::recvFileText);
               connect(this, &Widget::sendGroupMsg, broList, &BroList::recvGroupMsg);

               //聊天数据
               connect(broList, &BroList::sendId_Msg, this, [=](QString from_id, QString to_id, QString msg){
                   //群发消息
                   if (to_id.size() < 5) {
                       int type = 7;
                       int from_len = from_id.size();
                       int to_len = to_id.size();
                       m_t->write((char*)&type, 4);
                       m_t->write((char*)&from_len, 4);
                       m_t->write(from_id.toUtf8());
                       m_t->write((char*)&to_len, 4);
                       m_t->write(to_id.toUtf8());
                       m_t->write(msg.toUtf8());
                   } else {
                       int type = 5;
                       int from_len = from_id.size();
                       int to_len = to_id.size();
                       m_t->write((char*)&type, 4);
                       m_t->write((char*)&from_len, 4);
                       m_t->write(from_id.toUtf8());
                       m_t->write((char*)&to_len, 4);
                       m_t->write(to_id.toUtf8());
                       m_t->write(msg.toUtf8());
                   }
               });
               //文件数据
               static int fg = 0;
               connect(broList, &BroList::sendFileContent, this, [=](QString from, QString to, int size, QString data){
                  if (fg == 0) {
                      int type = 6;
                      int from_len = from.size();
                      int to_len = to.size();
                      m_t->write((char*)&type, 4);
                      m_t->write((char*)&from_len, 4);
                      m_t->write(from.toUtf8());
                      m_t->write((char*)&to_len, 4);
                      m_t->write(to.toUtf8());
                      m_t->write((char*)&size, 4);
                      m_t->write(data.toUtf8());
                  } else {
                      m_t->write(data.toUtf8());
                  }
                  fg++;

               });

               //发禁言等
               connect(broList, &BroList::sendTp, this, [=](QString tp){
                   int type = 8;
                   int tp_len = tp.size();
                   m_t->write((char*)&type, 4);
                   m_t->write((char*)&tp_len, 4);
                   m_t->write(tp.toUtf8());
               });
               connect(broList, &BroList::sendNoSay, this, [=](QString noSay){
                   int type = 9;
                   int noSay_len = noSay.size();
                   m_t->write((char*)&type, 4);
                   m_t->write((char*)&noSay_len, 4);
                   m_t->write(noSay.toUtf8());
               });
               connect(broList, &BroList::sendOpenSay, this, [=](QString openSay){
                   int type = 10;
                   int openSay_len = openSay.size();
                   m_t->write((char*)&type, 4);
                   m_t->write((char*)&openSay_len, 4);
                   m_t->write(openSay.toUtf8());
               });
           } else {
               QMessageBox::information(this, "提示", "帐号或密码错误");
           }
       } else if (type == 4) {//接收列表数据
           int pcnt;
           m_t->read((char*)&pcnt, 4);
           emit sendPcnt(pcnt);
           int sum;
           m_t->read((char*)&sum, 4);
           int len = 0;
           char id_[20]={0};
           for (int i = 0;i < sum; i++) {
               m_t->read((char*)&len, 4);
               m_t->read(id_, len);
               QString uid(id_);
               emit sendId(uid);
           }
           emit sendListOver();
       } else if (type == 5) {
           QByteArray msg_ =  m_t->readAll();
           QString msg = msg_;
           emit sendMsg(msg);
       } else if (type == 6) {
           int len;
           char data_[1024] = {0};
           m_t->read((char*)&len, 4);
           m_t->read(data_, len);
           QString data(data_);
           emit sendFileText(data);
           QByteArray all_ = m_t->readAll();
           QString all(all_);
           emit sendFileText(all);
       } else if (type == 7) {
           int len;
           char from_[20] = {0};
           m_t->read((char*)&len, 4);
           m_t->read(from_, len);
           QByteArray msg_ =  m_t->readAll();
           QString from(from_);
           QString msg = msg_;
           emit sendGroupMsg(from, msg);
       } else if (type == 8) {

       }

    });


}

Widget::~Widget()
{
    delete ui;
}

bool Widget::eventFilter(QObject *watched, QEvent *event)
{

    if (watched == ui->login && event->type() == QEvent::MouseButtonPress) {
        qDebug() << "OK ";
        logon *lg = new logon;
        lg->show();
        this->close();
    }
    if (watched == ui->updatepd && event->type() == QEvent::MouseButtonPress) {
        ModifyPd *mpd = new ModifyPd;
        mpd->show();
        this->close();
    }
}

void Widget::on_on_clicked()
{
    int type = 1;
    m_t->write((char*)&type, 4);
    //发送账户和密码用于验证
    QString c_num = ui->num->text();
    QString c_passwd = ui->passwd->text();
    if (QString::compare("", c_num)==0 || QString::compare("", c_passwd) == 0) {
        QMessageBox::information(this, "错误", "帐号或密码不能为空");
        return ;
    }
    qDebug() << c_num << " " << c_passwd;
    int c_num_len = c_num.size();
    int c_passwd_len = c_passwd.size();
    m_t->write((char *)&c_num_len, 4);
    m_t->write(c_num.toUtf8());
    m_t->write((char *)&c_passwd_len, 4);
    m_t->write(c_passwd.toUtf8());

}
